package com.jp.knowledge.view.experience;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.anarchy.classify.simple.widget.InsertAbleGridView;
import com.jp.knowledge.R;

import java.lang.reflect.Field;

/**
 * Created by 528787067@qq.com on 2017/1/22.
 */

public class SquareInsertAbleGridView extends InsertAbleGridView {

    private int sideLength;
    private int sideCount;
    private int sideGap;
//    private int outLinePadding;
//    private int innerPadding;

    public SquareInsertAbleGridView(Context context) {
        this(context, null);
    }

    public SquareInsertAbleGridView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SquareInsertAbleGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SquareInsertAbleGridView);

        sideLength = (int)(typedArray.getDimension(R.styleable.SquareInsertAbleGridView_sideLength, 0) + 0.5);
        sideCount = typedArray.getInt(R.styleable.SquareInsertAbleGridView_sideCount, 2);
        sideGap = typedArray.getInt(R.styleable.SquareInsertAbleGridView_sideGap, 10);
        typedArray.recycle();

//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SquareInsertAbleGridView);
//        outLinePadding = a.getDimensionPixelSize(R.styleable.InsertAbleGridView_OutlinePadding, 10);
//        innerPadding = a.getDimensionPixelOffset(R.styleable.InsertAbleGridView_InnerPadding, 10);
//        a.recycle();

        // 使用反射修改父类中的值
        try {
            Field mRowCountField = InsertAbleGridView.class.getDeclaredField("mRowCount");
            mRowCountField.setAccessible(true);
            mRowCountField.set(this, sideCount);
            mRowCountField.setAccessible(false);
            Field mColumnCountField = InsertAbleGridView.class.getDeclaredField("mColumnCount");
            mColumnCountField.setAccessible(true);
            mColumnCountField.set(this, sideCount);
            mColumnCountField.setAccessible(false);
            Field mRowGapField = InsertAbleGridView.class.getDeclaredField("mRowGap");
            mRowGapField.setAccessible(true);
            mRowGapField.set(this, sideGap);
            mRowGapField.setAccessible(false);
            Field mColumnGapField = InsertAbleGridView.class.getDeclaredField("mColumnGap");
            mColumnGapField.setAccessible(true);
            mColumnGapField.set(this, sideGap);
            mColumnGapField.setAccessible(false);
        } catch (Exception e) {
            return;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(sideLength <= 0)
            sideLength = widthMeasureSpec;
//        sideLength = MeasureSpec.makeMeasureSpec(sideLength, MeasureSpec.AT_MOST);
//
//        int childCount = getChildCount(), tempLength;
//        if(childCount <= 0)
//            return;
//        if(childCount == 1)
//            tempLength = sideLength - 2 * outLinePadding;
//        else
//            tempLength = (sideLength - 2 * (outLinePadding + innerPadding) - (sideCount-1) * sideGap)/sideCount;
//        tempLength = MeasureSpec.makeMeasureSpec(tempLength, MeasureSpec.EXACTLY);
//        for(int i = 0; i < childCount; i++){
//            getChildAt(i).measure(tempLength, tempLength);
//        }
//        tempLength = MeasureSpec.makeMeasureSpec(sideLength, MeasureSpec.EXACTLY);
//        setMeasuredDimension(tempLength, tempLength);
        super.onMeasure(sideLength, sideLength);
    }
}
